/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mfd2;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import fr.ove.openmath.jome.Jome;
import fr.ove.openmath.exceptions.*;
import fr.ove.openmath.mfd2.events.*;

public class PanelJomes extends Panel  implements OpenMathResultSetable {
    private Bullet bullet;
    
    // Everything about the request
    private Panel panelRequest;
    private Jome jomeRequest;

    // Everything about the result
    private Panel panelResult;
    private Jome jomeResult;
    
    // The listeners
    private Vector listeners = new Vector();

    /**
    * The constructor.
    */
	public PanelJomes() {
	    super();

	    // On met un BorderLayout.
	    // La zone "North" contiendra la requête
	    // La zone "Center" contiendra le résultat de la requête.
		setLayout(new BorderLayout(0, 0));

        Panel mainPanel = new Panel();
        mainPanel.setLayout(new BorderLayout(0, 0));
        add("Center", mainPanel);
        
		// La partie requête
		panelRequest = new Panel(new FlowLayout(FlowLayout.LEFT, 5, 4));
		
		jomeRequest = new Jome();
		jomeRequest.setDrawBounds(false);
		jomeRequest.setFont(new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, 18));
		jomeRequest.setShiftX(0);
		jomeRequest.setShiftY(0);
        panelRequest.add(jomeRequest);
        
		// La partie résultat
		panelResult = new Panel(new FlowLayout(FlowLayout.CENTER, 0, 0));
		
		jomeResult = new Jome();
		jomeResult.setDrawBounds(false);
		jomeResult.setShiftX(0);
		jomeResult.setShiftY(0);
		jomeResult.setFont(new java.awt.Font("Times New Roman",java.awt.Font.PLAIN,12));
		panelResult.add(jomeResult);
        
		mainPanel.add("North", panelRequest);
		mainPanel.add("Center", panelResult);
	    
		try {
    	    bullet = new Bullet(this);
            add("West", bullet);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
	}

    public boolean isSelected() {
        return bullet.isSelected();
    }
    
    public void setSelected(boolean isSelected) {
        bullet.setSelected(isSelected);
    }
        
	// La partie requête de la chose.

	/**
	* Sets the request. (under its linear form)
	* @param request the request.
	*/
	public void setLinearRequest(String request) {
	    if (!request.equals(jomeRequest.getLinear())) {
    	    jomeRequest.setLinear(request);
    	    
    	    PanelJomesEvent event = new PanelJomesEvent(this);
    	    event.setAction(PanelJomesEvent.UPDATE_REQUEST, null);
    	    firePanelJomesEvent(event);
    	}
	}

	/**
	* Returns the request. (under its linear form)
	*/
	public String getLinearRequest() {
	    return jomeRequest.getLinear();
	}

	/**
	* Sets the request. (under its OpenMath form)
	* @param request the request.
	*/
	public void setOpenMathRequest(String request) throws ErrorMessageException {
	    try {
    	    jomeRequest.setOpenMath(request);
	    
    	    PanelJomesEvent event = new PanelJomesEvent(this);
    	    event.setAction(PanelJomesEvent.UPDATE_REQUEST, null);
    	    firePanelJomesEvent(event);
    	}
    	catch(Exception e) {
    	    throw((ErrorMessageException) e);
    	}
	}
	
	/**
	* Returns the request. (under its OpenMath form)
	*/
	public String getOpenMathRequest() {
	    return jomeRequest.getOpenMath();
	}

	// La partie résultat de la chose.

	/**
	* Sets the result. (under its linear form)
	* @param result the result.
	*/
	public void setLinearResult(String result) {
	    jomeResult.setLinear(result);
	    
	    PanelJomesEvent event = new PanelJomesEvent(this);
	    event.setAction(PanelJomesEvent.UPDATE_RESULT, null);
	    firePanelJomesEvent(event);
	}

	/**
	* Returns the result. (under its linear form)
	*/
	public String getLinearResult() {
	    return jomeResult.getLinear();
	}
	
	public Jome getJomeResult() {
	    return jomeResult;
	}

	public Jome getJomeRequest() {
	    return jomeRequest;
	}

	/**
	* Sets the result. (under its OpenMath form)
	* @param result the result.
	*/
	public void setOpenMathResult(String result) throws ErrorMessageException  {
	    try {
    	    jomeResult.setOpenMath(result);
    	    
    	    PanelJomesEvent event = new PanelJomesEvent(this);
    	    event.setAction(PanelJomesEvent.UPDATE_RESULT, null);
    	    firePanelJomesEvent(event);
    	}
    	catch (Exception e) {
    	    throw((ErrorMessageException) e);
    	}
	}

	/**
	* Returns the result. (under its OpenMath form)
	*/
	public String getOpenMathResult() {
	    return jomeResult.getOpenMath();
	}
	
    /**
    */
    public void addPanelJomesListener(PanelJomesListener panelJomesListener) {
        listeners.addElement(panelJomesListener);
    }

    /**
    */
    public void removePanelJomesListener(PanelJomesListener panelJomesListener) {
        listeners.removeElement(panelJomesListener);
    }

    /**
    */
    public void removeAllPanelJomesListener() {
        listeners.setSize(0);
    }

    /**
    * Fires a PanelJomesEvent event to registered listeners.
    * @param panelJomesEvent event encapsulating relevant information.
    */
    public void firePanelJomesEvent(PanelJomesEvent panelJomesEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((PanelJomesListener)listeners.elementAt(i)).consumePanelJomesEvent(panelJomesEvent);
    }
    
    
    private class Bullet extends Panel {
        PanelJomes panelJomes;
        int width = 8;
        int minHeight = 15;
        boolean isSelected = true;
        
        public Bullet(PanelJomes pj) throws Exception {
            panelJomes = pj;
            
            Font font = pj.getJomeRequest().getFont();
            FontMetrics fm = getFontMetrics(font);
            minHeight = fm.getHeight() + 4;
            addMouseListener(
                new MouseAdapter() {
                    public void mousePressed(MouseEvent event) {
                	    PanelJomesEvent panelJomesEvent = new PanelJomesEvent(panelJomes);
                	    panelJomesEvent.setAction(PanelJomesEvent.UPDATE, null);
                	    panelJomes.firePanelJomesEvent(panelJomesEvent);
                	    
                	    panelJomesEvent.setAction(PanelJomesEvent.UPDATE_SELECTION, null);
                	    panelJomes.firePanelJomesEvent(panelJomesEvent);
                    }
                }
            );
        }
        
        public Dimension getPreferredSize() {
            return new Dimension(width, (int) Math.max(minHeight, (panelJomes.getSize()).height));
        }
        
        public boolean isSelected() {
            return isSelected;
        }
        
        public void setSelected(boolean isSelected) {
            this.isSelected = isSelected;
            repaint();
        }
        
        public void paint(Graphics g) {
            Dimension size = getPreferredSize();
            if (isSelected)
                g.setColor(Color.red);
            else
                g.setColor(Color.black);
            
            g.fillRect(4, 2, 2, size.height - 4);
            g.fillRect(4, 2, width, 2);
            g.fillRect(4, size.height - 4, width, 2);
        }
    }


}
